package com.extension.company.view.details;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.widget.MyWebView;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.view.adapter.RecommendAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;


/**
 * 商品详情页 --第二楼，商品详情页
 */
public class SecondFloorFragment extends Fragment {
	private LayoutInflater inflater;
	private Context mContext;
	private List<String> list = new ArrayList<>();
	private String richString = "<p><img alt=Image src=/timg?image&quality=80&size=b9999_10000&sec=1565768729&di=2a88d9f8ba48401901a2de2fb7cb76b6&imgtype=jpg&er=1&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F4610b912c8fcc3cef70d70409845d688d53f20f7.jpg width=750 height=924><img alt=Image src=/uploads/20190728/0b56eafa3099774c97e444b9b209cd45.jpg width=750 height=935><img alt=Image src=/uploads/20190728/04bf7a4a64ff0f3a69e12d8e7dedb2d6.jpg width=750 height=507><img alt=Image src=/uploads/20190728/4719c3a376ee38e57b63712231191042.jpg width=750 height=988><img alt=Image src=/uploads/20190728/6aa38a9f163dd30af71e4f5e9f8fed4b.jpg width=750 height=992><br></p>";
	private WebView webView;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.mContext = context;
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		this.inflater = inflater;
		View rootView = inflater.inflate(R.layout.fragment_second_floor, null);
		initView(rootView);
		return rootView;
	}

	public void initView(View rootView) {

		RecyclerView recyclerView = rootView.findViewById(R.id.some_recommend);
		LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
		linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		recyclerView.setLayoutManager(linearLayoutManager);
		recyclerView.addItemDecoration(new RecyclerViewSpacesItemDecoration(30));
		recyclerView.setAdapter(new RecommendAdapter(mContext, list));

		webView = rootView.findViewById(R.id.webView);
		WebSettings webSettings = webView.getSettings();
		webSettings.setDisplayZoomControls(false); //隐藏webview缩放按钮
		webSettings.setJavaScriptEnabled(true);//支持js
		webSettings.setBuiltInZoomControls(true); // 显示放大缩小
		webSettings.setUseWideViewPort(true);
		webSettings.setBlockNetworkImage(false);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setSupportZoom(true); // 可以缩放
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			//允许混合（http，https）
			//websettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
			webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
		}

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return true;
			}
		});

		webView.loadDataWithBaseURL(RetrofitUrlApi.BaseHostUrl, getNewContent(richString), "text/html", "UTF-8", null);
	}

	public static String getNewContent(String htmltext) {
		Document doc = Jsoup.parse(htmltext);
		Elements elements = doc.getElementsByTag("img");
		for (Element element : elements) {
			element.attr("width", "100%").attr("height", "auto");
		}
		return doc.toString();
	}
}
